// 定义一个数值和字符串的联合类型
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var objNum = [1, 2, '1', '2'];
console.log(objNum);
// 定义一个类型断言
var dyarr = [1, 2, 3, 4];
var dy = dyarr.length;
console.log(dy);
// 定义一个 void 实现函数的无返回值
function fnc() {
    console.log("好好学习天天向上");
}
fnc();
// 定义一个 never 的函数
function infiniteLoop() {
    while (true) {
    }
}
// 分别定义 es5 和 ts 的函数定义
function fnkx(a, b) {
    if (b) {
        return console.log(a + b);
    }
    else {
        return console.log(a);
    }
}
fnkx(1);
function fn(a, b) {
    return console.log(a + b);
}
fn(1, 2);
function fn1(a1, b1) {
    if (b1 === void 0) { b1 = 'b1'; }
    return console.log(a1 + '' + b1);
}
fn1("a1");
function buildName(firstName) {
    var restOfName = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        restOfName[_i - 1] = arguments[_i];
    }
    return console.log(firstName + "" + restOfName.join(""));
}
buildName("Joseph", "Samuel", "Lucas", "MacKinzie");
// 实现一个函数重载
var suits = ["hearts", "spades", "clubs", "diamonds"];
function pickCard(x) {
    if (typeof x == "object") {
        var pickedCard = Math.floor(Math.random() * x.length);
        return pickedCard;
    }
    else if (typeof x == "number") {
        var pickedSuit = Math.floor(x / 13);
        return { suit: suits[pickedSuit], card: x % 13 };
    }
}
var myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
var pickedCard1 = myDeck[pickCard(myDeck)];
alert("card: " + pickedCard1.card + " of " + pickedCard1.suit);
var pickedCard2 = pickCard(15);
alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);
// ts 中定义一个类并实现继承
var Animal = /** @class */ (function () {
    function Animal() {
    }
    Animal.prototype.move = function (distanceInMeters) {
        if (distanceInMeters === void 0) { distanceInMeters = 0; }
        console.log("Animal moved ".concat(distanceInMeters, "m."));
    };
    return Animal;
}());
var Dog = /** @class */ (function (_super) {
    __extends(Dog, _super);
    function Dog() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Dog.prototype.bark = function () {
        console.log('Woof! Woof!');
    };
    return Dog;
}(Animal));
var dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();
// 2种方式定义一个数组（数组中的元素可以是任意类型）
var arr = [1, "one", true];
var arr1 = [1, 'as', true];
var arr2 = [
    { a: 1, b: "as", c: true },
    { a: 2, b: "bs", c: false }
];
// Ts定义一个求和函数;例Add(2,8,9,6,5,8,7),返回结果45
function Add(val1) {
    var val = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        val[_i - 1] = arguments[_i];
    }
    var sum = 0;
    sum += val1;
    if (val)
        val.forEach(function (item, index) {
            sum += item;
        });
    return sum;
}
console.log(Add(2, 8, 9, 6, 5, 8, 7));
// Ts定义一个函数返回值为boolean类型的判断函数;例isTrue(nudefined),返回false
function isTrue(val) {
    switch (typeof val) {
        case "number":
            return true;
            break;
        case "undefined":
            return false;
            break;
        case "object":
            return true;
            break;
        default:
            break;
    }
}
console.log(isTrue(undefined));
// 使用函数编写，实现统计指定字符串中指定字符出现的次数，字符串和要统计的字母作为参数传递。str="acdkjcdeicl"  ;  strCount(str, “c”) 
// 思路：计数器： 遍历每一个字符。判断是否和val全等，如果全等，计数器加一。
function sumItem(val, ele) {
    var num = 0;
    var arr = val.split("");
    arr.forEach(function (item, index) {
        if (item === ele) {
            num += 1;
        }
    });
    return num;
}
console.log(sumItem("asdededsfg", "e"));
console.log(sumItem("qwasdederqwdsfg", "e"));
// 数字（乘5）添加：
// 将arrs中所有的数字乘以5 var arrs = [‘a’,’c’,6,’e’,5,3,’d’]，返回[30,25,15]
function Addnum(val) {
    var newArr = [];
    val.forEach(function (v, i) {
        if (typeof v === 'number') {
            newArr.push(v * 5);
        }
    });
    return newArr;
}
var arrs = ["a", "c", 6, "e", 5, 3, "d"];
var arrq = ["a", 12, 6, "e", 5, 3, "d"];
console.log(Addnum(arrs));
console.log(Addnum(arrq));
// 补零：
// 写一个函数，将这个数组中所有的一位数前面补0，输出一个新数组，即最后返回。
function Adderxo(val) {
    var newArr = [];
    val.forEach(function (item, index) {
        var items = "0" + item;
        newArr.push(items);
    });
    return newArr;
}
var arro = [2, 3, 45, 6, 7];
console.log(Adderxo(arro));
// 找真值：
// 写一个函数bouncer（），实现将数组中的真值，添加到一个新数组中。
function bouncer(val) {
    var newArr = [];
    val.forEach(function (v, i) {
        if (Boolean(v) && typeof v !== 'boolean') {
            newArr.push(v);
        }
    });
    return newArr;
}
var arra = ['1', 2, 'asw', 45, true, "", 0];
console.log(bouncer(arra));
console.log(bouncer([7, "ate", "", false, 9]));
